package com.powernode.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.powernode.common.Constants;
import com.powernode.utils.WebMvcUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 内容: 生成验证码
 * 作者:
 */
@Controller
public class CaptchaController {
    /**
     * 写一个验证码出去
     */
    @RequestMapping("captcha.do")
    public void captcha(HttpServletResponse response){
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 40, 1, 100);
        String code = lineCaptcha.getCode();
        //将验证码在session中保存一份
        WebMvcUtils.getSession().setAttribute(Constants.CAPTCHA_CODE,code);
        try {
            lineCaptcha.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
